package com.tle.libs;

import java.util.ArrayList;
import java.util.Locale;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.tle.eventexample.R;
import com.tle.eventexample.Setting;
import com.tle.libs.CONSTANTS.ACTION_TYPE;
import com.tle.libs.CONSTANTS.FONT_PATHS;
import com.tle.libs.CONSTANTS.TEXT_SIZES;

public class UIUtilities 
{
	////////////////////////////////////////////////////////////////////////////////////////////////////
	// Apply UI standard from CONSTANTS //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	public static void setUIStandard (TextView textView , FONT_PATHS paths , TEXT_SIZES size)
	{
		if(paths == null || size == null || textView == null)
			return;
		
		Context context = textView.getContext();
		
		textView.setTextSize(size.getValue());
		Typeface typeface = Typeface.createFromAsset(context.getAssets(), paths.getValue());
		textView.setTypeface(typeface);
	}
	
	protected static void setUIStandard (View view , FONT_PATHS paths , TEXT_SIZES size)
	{
		TextView textView = (TextView)view;
		setUIStandard(textView , paths , size);
	}
	
	public static void setUIStandard (LinearLayout linearLayout , FONT_PATHS paths , TEXT_SIZES size)
	{
		for (int nItemIndex=0 ; nItemIndex<linearLayout.getChildCount() ; nItemIndex ++)
		{
			View childView = linearLayout.getChildAt(nItemIndex);
						
			if(childView instanceof TextView)
				setUIStandard(childView , paths , size);
			else if (childView instanceof LinearLayout || childView instanceof TableLayout)
			{
				LinearLayout subLinearLayout = (LinearLayout) childView;
				setUIStandard(subLinearLayout , paths , size);
			}
			
		}
	}


	
	/**
	 * This method is not effected
	 * @param activity
	 * @param paths
	 * @param sizes
	 */
	public static void setActivityStandard (Activity activity , FONT_PATHS paths , TEXT_SIZES sizes)
	{
		if (activity == null || paths == null || sizes == null)
			return;
				
		SpannableString s = new SpannableString(activity.getTitle());
		Typeface typeface = Typeface.createFromAsset(activity.getAssets() , paths.getValue());
		s.setSpan(typeface, 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


		
		ActionBar actionBar = activity.getActionBar();
		actionBar.setTitle(s);
	}

	
	
	
	
	public static void changedLanguage (Context context, String sLanguage, ILanguage iLanguage)
	{
		Locale locale = new Locale(sLanguage); 
		Locale.setDefault(locale);
		Configuration config = new Configuration();
		config.locale = locale;
		context.getApplicationContext().getResources().updateConfiguration(config, null);
		
		iLanguage.updateLanguage();
	}
	
	static public interface ILanguage 
	{
		public void updateLanguage ();
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
																  // Apply UI standard from CONSTANTS //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	
	
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	// Building UI //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	public static LinearLayout buildLayoutWith2Cols (View view1, LayoutParams layoutParams1, View view2, LayoutParams layoutParams2)
	{
		if (view1 == null || view2 == null || layoutParams1 == null || layoutParams2 == null)
			return null;
		
		Context context = view1.getContext();
		
		LinearLayout resLayout = new LinearLayout(context);
		resLayout.setOrientation(LinearLayout.HORIZONTAL);
		
		resLayout.addView(view1 , layoutParams1);
		resLayout.addView(view2 , layoutParams2);
		
		return resLayout;
	}
	
	
	
	/**
	 * 
	 * @param arrComponents
	 * @param rowParams
	 * @param arrItemParams: 1 -> apply for all, this size is the same arrComponents
	 * @return TableRow added array components
	 */
	public static TableRow buildRow (ArrayList<TextView> arrComponents, TableLayout.LayoutParams rowParams, ArrayList<TableRow.LayoutParams> arrItemParams)
	{
		if (arrComponents == null || arrComponents.size() == 0 || arrItemParams == null || arrItemParams.size() == 0)
			return null;
		
		Context context = arrComponents.get(0).getContext();
		
		TableRow tableRow = new TableRow(context);
		tableRow.setLayoutParams(rowParams);
		
		if (arrItemParams.size() == 1)
		{
			TableRow.LayoutParams itemParams = arrItemParams.get(0);
			for (int nItemIndex=0 ; nItemIndex<arrComponents.size() ; nItemIndex++)
				tableRow.addView(arrComponents.get(nItemIndex), itemParams);
		}
		else
		{
			if ( arrItemParams.size() != arrComponents.size() )
				return null;
			
			for (int nItemIndex=0 ; nItemIndex<arrComponents.size() ; nItemIndex++)
				tableRow.addView(arrComponents.get(nItemIndex), arrItemParams.get(nItemIndex));

		}
		
		return tableRow;
	}
	
	public static HorizontalScrollView buildRadioButtons (ArrayList<RadioButton> arrRadioButtons , RadioGroup.LayoutParams layoutParams , int nItemIndexSelected)
	{
		if (arrRadioButtons == null || layoutParams == null)
			return null;
		
		if (nItemIndexSelected < 0 || nItemIndexSelected >= arrRadioButtons.size())
			return null;
	
		RadioButton rdoChecked = arrRadioButtons.get(nItemIndexSelected);
		Context context = rdoChecked.getContext();
		
		RadioGroup rdoGroup = new RadioGroup(context);
		rdoGroup.setOrientation(RadioGroup.HORIZONTAL);
		
		for (int nItemIndex=0 ; nItemIndex<arrRadioButtons.size() ; nItemIndex++)
			rdoGroup.addView(arrRadioButtons.get(nItemIndex), layoutParams);
		
		rdoChecked.setId(0);
		rdoGroup.check(0);
		
		HorizontalScrollView scrollView = new HorizontalScrollView(rdoChecked.getContext());
		scrollView.addView(rdoGroup);
		scrollView.setLayoutParams( new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) );
				
		return scrollView;
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
																  					   // Building UI //
	////////////////////////////////////////////////////////////////////////////////////////////////////

	
	
	
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	// Show/Hide UI //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * Hides the soft keyboard
	 */
	static public void hideSoftKeyboard(Activity activity) 
	{
	    if(activity.getCurrentFocus() != null) 
	    {
	        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
	        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
	    }
	}

	/**
	 * Shows the soft keyboard
	 */
	static public void showSoftKeyboard(Activity activity, View view) 
	{
	    InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
	    view.requestFocus();
	    inputMethodManager.showSoftInput(view, 0);
	}
	
	
	
	
	
	static public void showQuitDialog (Activity activity)
	{
		AlertDialog.Builder dlg = new AlertDialog.Builder(activity);
		
		dlg.setTitle(R.string.const_Confirm);
		dlg.setMessage(R.string.const_ConfirmQuestion);
		dlg.setPositiveButton(R.string.const_ConfirmYes, new AlertDialogHandler(activity, ACTION_TYPE.YES) );
		dlg.setNegativeButton(R.string.const_ConfirmNo, new AlertDialogHandler(activity, ACTION_TYPE.NO) );
		
		dlg.create().show();
		
	}
		
	static private class AlertDialogHandler implements DialogInterface.OnClickListener
	{
		ACTION_TYPE m_actionType = ACTION_TYPE.NONE;
		Activity m_Activity;
		
		public AlertDialogHandler(Activity activity, ACTION_TYPE actionType) 
		{
			m_actionType = actionType;
			m_Activity = activity;
		}
		
		@Override
		public void onClick(DialogInterface dialog, int which) 
		{
			switch (m_actionType)
			{
			case YES:
				{
					m_Activity.finish();
				}
				break;
				
			default:
				dialog.cancel();
			}
			
		}
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
																					  // Show/Hide UI //
	////////////////////////////////////////////////////////////////////////////////////////////////////
}
